function escapeRegExp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
exports.findWithCounts = function (body, withoutCounts) {
    console.log(JSON.stringify("requestLog findWithCounts", body));
    let model = this;
    let options = body.options || {};
    delete body.options;
    let skip = parseInt(options.skip) || 0;
    let limit = parseInt(options.limit) || 10;
    let sort = options.sort || {_id: -1};
    if (options.likeArr) {
        options.likeArr.forEach(function (likeKey) {
            if (body[likeKey] !== undefined)
                body[likeKey] = new RegExp(escapeRegExp(body[likeKey]))
        })
    }
    if (options.dateParams) {
        for (let dateKey in options.dateParams) {
            if (!body[dateKey]) {
                let dateP = {};
                for (let key in options.dateParams[dateKey])
                    dateP[key] = new Date(options.dateParams[dateKey][key]);
                body[dateKey] = dateP;
            }
        }
    }
    printLog(body);
    if (withoutCounts) {
        return new Promise(function (resolve,reject) {
            model.find(body,{},{skip:skip,limit:limit,sort:sort},function (err,result) {
                if(err){
                    reject({code: 3, errorInfo: err.message});
                }else{
                    resolve(result);
                }
            })
        });
    } else {
        return new Promise(function (resolve,reject) {
            model.find(body).count(function (countErr,count) {
                if(countErr){
                    reject({code: 3, errorInfo: countErr.message});
                }else{
                    model.find(body,{},{skip:skip,limit:limit,sort:sort},function (findErr,data) {
                        if(findErr){
                            reject({code: 3, errorInfo: findErr.message});
                        }else{
                            resolve({data: data, count: count});
                        }
                    });
                }
            });
        });
    }
};
